package com.gzhryc.shared_device.oem.h5.mng.controllers.member;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.NumberTools;
import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.DateBranchHelper;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.base.enums.EOperatorType;
import com.gzhryc.shared_device.base.models.OperateInfo;
import com.gzhryc.shared_device.oem.code.member.dao.db.Member;
import com.gzhryc.shared_device.oem.code.member.dao.db.MemberWallet;
import com.gzhryc.shared_device.oem.code.member.dao.db.MemberWalletOperate;
import com.gzhryc.shared_device.oem.code.member.services.MemberService;
import com.gzhryc.shared_device.oem.code.member.services.MemberWalletOperateService;
import com.gzhryc.shared_device.oem.code.member.services.MemberWalletService;
import com.gzhryc.shared_device.oem.code.member.services.dto.MemberWalletOperateSearch;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;
import com.gzhryc.system.SysConstants;
import com.gzhryc.system.dao.db.Manager;

public class MemberWalletController {

    static Logger log = Logger.getLogger(MemberWalletController.class);

    @Route(value = { "/member/wallet/info.html" })
    public String wallet(HttpServletRequest request) {
        Long id = WebUtils.getLong(request,"id");
        Member member = MemberService.self().getById(id);
        if(member != null) {
            WebUtils.setAttribute(request, member);
            MemberWallet wallet = MemberWalletService.self().getByMemberId(member.getId());
            if(wallet != null){
                request.setAttribute("wallet",wallet);
            }
        }
        request.setAttribute("dateBranchItemList", DateBranchHelper.self().getMonthDateBranchItems());
        return "/member/wallet/info.html";
    }

    @Route(value = "/member/wallet/operate/list.data")
    public ListResult<MemberWalletOperate> listData(HttpServletRequest request) {
        ListResult<MemberWalletOperate> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        MemberWalletOperateSearch search = WebUtils.getParameter(request,MemberWalletOperateSearch.class);
        if(search.getMemberId() != null) {
            Integer pageNo = WebUtils.getInteger(request, "page", 1);
            Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);
            
            Date dateBranch = new Date();
            if(StringTools.isNotBlank(search.getDateBranchId())){
                dateBranch = DateBranchHelper.getDateByItemId(search.getDateBranchId());
            }

            result.getBody().setTotal(MemberWalletOperateService.self(dateBranch).countBySearch(search));
            List<MemberWalletOperate> operateList = MemberWalletOperateService.self(dateBranch).findBySearch(search, pageNo, pageLimit);
            if (operateList != null && operateList.size() > 0) {
                result.setCode(DtoConstants.SUCCESS, null);
                result.getBody().setData(operateList);
            }
        }
        return result;
    }

    @Route(value = { "/member/wallet/unfreeze.html" })
    public String unfreeze(HttpServletRequest request) {
        Long memberId = WebUtils.getLong(request,"memberId");
        Member member = MemberService.self().getById(memberId);
        if(member != null) {
            request.setAttribute("member", member);
            MemberWallet wallet = MemberWalletService.self().getByMemberId(memberId);
            WebUtils.setAttribute(request, wallet);
        }
        return "/member/wallet/unfreeze.html";
    }

    @Route(value = { "/member/wallet/unfreeze.do" })
    public AjaxResult doUnfreeze(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "解冻金额失败");
        Long memberId = WebUtils.getLong(request,"memberId");
        Double unfreezeMoney = WebUtils.getDouble(request,"unfreezeMoney");
        String note = WebUtils.getString(request,"note");
        if(memberId != null && unfreezeMoney != null && unfreezeMoney > 0 && unfreezeMoney < 9999){
            Manager manager = WebUtils.getAttribute(request, SysConstants.LOGIN_MANAGER, Manager.class);
            OperateInfo operateInfo = new OperateInfo(WebConstants.PROGRAM_TYPE,WebConstants.PROGRAM_TYPE_NAME);
            operateInfo.setOperator( EOperatorType.Manager.index(),manager.getFullName());
            operateInfo.setOperateNote(note);
            try {
                if (MemberWalletService.self().unfreezeMoney(memberId, NumberTools.changeMoneyLi(unfreezeMoney),operateInfo)){
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

    @Route(value = { "/member/wallet/freeze.html" })
    public String freeze(HttpServletRequest request) {
        Long memberId = WebUtils.getLong(request,"memberId");
        Member member = MemberService.self().getById(memberId);
        if(member != null) {
            request.setAttribute("member",member);

            MemberWallet wallet = MemberWalletService.self().getByMemberId(memberId);
            WebUtils.setAttribute(request, wallet);
        }
        return "/member/wallet/freeze.html";
    }

    @Route(value = { "/member/wallet/freeze.do" })
    public AjaxResult doFreeze(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "冻结金额失败");
        Long memberId = WebUtils.getLong(request,"memberId");
        Double unfreezeMoney = WebUtils.getDouble(request,"unfreezeMoney");
        String note = WebUtils.getString(request,"note");
        if(memberId != null && unfreezeMoney != null && unfreezeMoney > 0 && unfreezeMoney < 9999){
            Manager manager = WebUtils.getAttribute(request, SysConstants.LOGIN_MANAGER, Manager.class);
            OperateInfo operateInfo = new OperateInfo(WebConstants.PROGRAM_TYPE,WebConstants.PROGRAM_TYPE_NAME);
            operateInfo.setOperator( EOperatorType.Manager.index(),manager.getFullName());
            operateInfo.setOperateNote(note);
            try {
                if (MemberWalletService.self().freezeMoney(memberId, NumberTools.changeMoneyLi(unfreezeMoney),operateInfo)){
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

}
